package com.novelot.plugin.allinone.switc;

import com.novelot.plugin.allinone.util.Logger;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class MyCellRenderer extends JPanel implements ListCellRenderer<SwitchBean> {

    private final JLabel labelKey;
    private final JCheckBox checkBox;
    private final DefaultListModel listModel;
    private final JList<SwitchBean> listView;

    public MyCellRenderer(JList<SwitchBean> listView, DefaultListModel listModel) {
        this.listView = listView;
        this.listModel = listModel;

        setLayout(new GridBagLayout());

        //key
        labelKey = new JLabel();

        GridBagConstraints constraintsLabelKey = new GridBagConstraints();
        constraintsLabelKey.gridx = 1;
        constraintsLabelKey.gridy = 1;
        constraintsLabelKey.gridwidth = 1;
        constraintsLabelKey.gridheight = 1;
        constraintsLabelKey.weightx = 1;
        constraintsLabelKey.weighty = 1;
        constraintsLabelKey.fill = GridBagConstraints.HORIZONTAL;
        constraintsLabelKey.insets = new Insets(8, 8, 8, 8);

        this.add(labelKey, constraintsLabelKey);

        //value
        checkBox = new JCheckBox();
        GridBagConstraints constraintsCheckBox = new GridBagConstraints();
        constraintsCheckBox.gridx = 2;
        constraintsCheckBox.gridy = 1;
        constraintsCheckBox.gridwidth = 0;
        constraintsCheckBox.gridheight = 1;
        constraintsCheckBox.insets = new Insets(8, 8, 8, 8);
        this.add(checkBox, constraintsCheckBox);
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends SwitchBean> jList, SwitchBean switchBean, int position, boolean b, boolean b1) {
        labelKey.setText(switchBean.key);
        checkBox.setText(switchBean.offon ? "开" : "关");
        return this;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.GRAY);
        g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
    }
}
